package com.leadership.entity.project;

import java.beans.Transient;
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.leadership.entity.user.User;

/**
 * @author haiyan_xiao
 *
 */
@Entity
@Table(name = "user_project")
public class UserProject {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private User user;

    /** 培训项目名 */
    private Long projectId;

    /** 自我觉察是否完成 */
    @Column(name = "is_self_awareness_completed")
    private boolean selfAwarenessCompleted;

    /** 必修课程1是否完成 */
    @Column(name = "is_required_course_one_completed")
    private boolean requiredCourseOneCompleted;

    /** 必修课程2是否完成 */
    @Column(name = "is_required_course_two_completed")
    private boolean requiredCourseTwoCompleted;

    /** 必修课程3是否完成 */
    @Column(name = "is_required_course_three_completed")
    private boolean requiredCourseThreeCompleted;

    /** 必修课程4是否完成 */
    @Column(name = "is_required_course_four_completed")
    private boolean requiredCourseFourCompleted;

    /** 选修课程1是否完成 */
    @Column(name = "is_optional_course_one_completed")
    private boolean optionalCourseOneCompleted;

    /** 选修课程2是否完成 */
    @Column(name = "is_optional_course_two_completed")
    private boolean optionalCourseTwoCompleted;

    /** 选修课程3是否完成 */
    @Column(name = "is_optional_course_three_completed")
    private boolean optionalCourseThreeCompleted;

    /** 选修课程4是否完成 */
    @Column(name = "is_optional_course_four_completed")
    private boolean optionalCourseFourCompleted;

    /** 选修课程5是否完成 */
    @Column(name = "is_optional_course_five_completed")
    private boolean optionalCourseFiveCompleted;
    
    /** 培养关-修炼功法 */
    @Column(name = "is_practice_completed")
    private boolean practiceCompleted;

    /** 认证关是否完成 */
    @Column(name = "is_certificate_completed")
    private boolean certificateCompleted;

    /** 评估关是否完成 */
    @Column(name = "is_evaluation_completed")
    private boolean evaluationCompleted;

    /** 创建时间 */
    private Date createTime;
    
    private Long operator;

    @Column(name = "is_disabled")
    private boolean disabled;

    public UserProject() {
        
    }

    public UserProject(User user, Long projectId, Long operator) {
        this.user = user;
        this.projectId = projectId;
        this.selfAwarenessCompleted = false;
        this.requiredCourseOneCompleted = false;
        this.requiredCourseTwoCompleted = false;
        this.requiredCourseThreeCompleted = false;
        this.requiredCourseFourCompleted = false;
        this.optionalCourseOneCompleted = false;
        this.optionalCourseTwoCompleted = false;
        this.optionalCourseThreeCompleted = false;
        this.optionalCourseFourCompleted = false;
        this.optionalCourseFiveCompleted = false;
        this.certificateCompleted = false;
        this.evaluationCompleted = false;
        this.operator = operator;
        this.createTime = new Date();
    }

    public UserProject(User user, Long projectId,
            boolean selfAwarenessCompleted,
            boolean requiredCourseOneCompleted, boolean requiredCourseTwoCompleted,
            boolean requiredCourseThreeCompleted, boolean requiredCourseFourCompleted,
            boolean optionalCourseOneCompleted, boolean optionalCourseTwoCompleted,
            boolean optionalCourseThreeCompleted, boolean optionalCourseFourCompleted,
            boolean optionalCourseFiveCompleted,
            boolean certificateCompleted, boolean evaluationCompleted) {
        this.user = user;
        this.projectId = projectId;
        this.selfAwarenessCompleted = selfAwarenessCompleted;
        this.requiredCourseOneCompleted = requiredCourseOneCompleted;
        this.requiredCourseTwoCompleted = requiredCourseTwoCompleted;
        this.requiredCourseThreeCompleted = requiredCourseThreeCompleted;
        this.requiredCourseFourCompleted = requiredCourseFourCompleted;
        this.optionalCourseOneCompleted = optionalCourseOneCompleted;
        this.optionalCourseTwoCompleted = optionalCourseTwoCompleted;
        this.optionalCourseThreeCompleted = optionalCourseThreeCompleted;
        this.optionalCourseFourCompleted = optionalCourseFourCompleted;
        this.optionalCourseFiveCompleted = optionalCourseFiveCompleted;
        this.certificateCompleted = certificateCompleted;
        this.evaluationCompleted = certificateCompleted;
        this.createTime = new Date();
    }

    /**
     * 学习进度
     * @return
     */
    public BigDecimal getTotalCompletePercent() {
        int grade = 0;
        
        if(this.user.isHeartMethodCompleted()) {
            grade = grade + 10;
        }
        if(this.user.isMapCompleted()) {
            grade = grade + 10;
        }
        if(this.selfAwarenessCompleted) {
            grade = grade + 10;
        }
        if(this.isCourseCompleted()) {
            grade = grade + 35;
        }
        if(this.isPracticeCompleted()) {
            grade = grade + 35;
        }
        
        BigDecimal result = new BigDecimal((float)grade/100);
        return result.setScale(2, BigDecimal.ROUND_HALF_UP);
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public boolean isCompleted() {
        return this.selfAwarenessCompleted && this.requiredCourseOneCompleted 
                                           && this.requiredCourseTwoCompleted 
                                           && this.requiredCourseThreeCompleted
                                           && this.requiredCourseFourCompleted
                                           && this.optionalCourseOneCompleted
                                           && this.optionalCourseTwoCompleted
                                           && this.optionalCourseThreeCompleted
                                           && this.optionalCourseFourCompleted
                                           && this.optionalCourseFiveCompleted
                                           && this.practiceCompleted
                                           && this.certificateCompleted
                                           && this.evaluationCompleted;
    }
    
    @Transient
    public String getTotalStatus() {
        if(this.isCompleted()) {
            return "finished";
        } else if (!this.selfAwarenessCompleted && !this.requiredCourseOneCompleted 
                && !this.requiredCourseTwoCompleted 
                && !this.requiredCourseThreeCompleted
                && !this.requiredCourseFourCompleted
                && !this.optionalCourseOneCompleted
                && !this.optionalCourseTwoCompleted
                && !this.optionalCourseThreeCompleted
                && !this.optionalCourseFourCompleted
                && !this.optionalCourseFiveCompleted
                && !this.practiceCompleted
                && !this.certificateCompleted
                && !this.evaluationCompleted) {
            return "progress";
        } else {
            return "unopened";
        }
    }

    /**
               * 培养关-燎原计划是否完成
     * @return
     */
    public boolean isPlanCompleted() {
        return this.isCourseCompleted() && this.isPracticeCompleted();
    }

    /**
               * 传授武功是否完成
     * @return
     */
    public boolean isCourseCompleted() {
        if(this.requiredCourseOneCompleted && this.requiredCourseTwoCompleted
                && this.requiredCourseThreeCompleted && this.requiredCourseFourCompleted) {
            int optionalCompletedAmount = 0;
            if(this.optionalCourseOneCompleted) {
                optionalCompletedAmount++;
            }
            if(this.optionalCourseTwoCompleted) {
                optionalCompletedAmount++;
            }
            if(this.optionalCourseThreeCompleted) {
                optionalCompletedAmount++;
            }
            if(this.optionalCourseFourCompleted) {
                optionalCompletedAmount++;
            }
            if(this.optionalCourseFiveCompleted) {
                optionalCompletedAmount++;
            }
            
            return optionalCompletedAmount >= 3;
        } else {
            return false;
        }
    }

    /**
               * 必修完成率
     * @return
     */
    public BigDecimal getRequiredCourseCompletePercent() {
        int completedAmount = 0;
        if(this.requiredCourseOneCompleted) {
            completedAmount++;
        }
        if(this.requiredCourseTwoCompleted) {
            completedAmount++;
        }
        if(this.requiredCourseThreeCompleted) {
            completedAmount++;
        }
        if(this.requiredCourseFourCompleted) {
            completedAmount++;
        }
        
        BigDecimal result = new BigDecimal((float)completedAmount/4);
        return result.setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 选修完成率
     * @return
     */
    public BigDecimal getOptionalCourseCompletePercent() {
        int completedAmount = 0;
        if(this.optionalCourseOneCompleted) {
            completedAmount++;
        }
        if(this.optionalCourseTwoCompleted) {
            completedAmount++;
        }
        if(this.optionalCourseThreeCompleted) {
            completedAmount++;
        }
        if(this.optionalCourseFourCompleted) {
            completedAmount++;
        }
        if(this.optionalCourseFiveCompleted) {
            completedAmount++;
        }
        if(completedAmount >= 3) {
            return new BigDecimal(1);
        } else {
            BigDecimal result = new BigDecimal((float)completedAmount/3);
            return result.setScale(2, BigDecimal.ROUND_HALF_UP);
        }
    }
    
    public UserProject changeOperator(Long operator) {
        this.operator = operator;
        return this;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the user
     */
    public User getUser() {
        return user;
    }

    /**
     * @return the projectId
     */
    public Long getProjectId() {
        return projectId;
    }

    /**
     * @return the selfAwarenessCompleted
     */
    public boolean isSelfAwarenessCompleted() {
        return selfAwarenessCompleted;
    }

    /**
     * @return the requiredCourseOneCompleted
     */
    public boolean isRequiredCourseOneCompleted() {
        return requiredCourseOneCompleted;
    }

    /**
     * @return the requiredCourseTwoCompleted
     */
    public boolean isRequiredCourseTwoCompleted() {
        return requiredCourseTwoCompleted;
    }

    /**
     * @return the requiredCourseThreeCompleted
     */
    public boolean isRequiredCourseThreeCompleted() {
        return requiredCourseThreeCompleted;
    }

    /**
     * @return the requiredCourseFourCompleted
     */
    public boolean isRequiredCourseFourCompleted() {
        return requiredCourseFourCompleted;
    }

    /**
     * @return the optionalCourseOneCompleted
     */
    public boolean isOptionalCourseOneCompleted() {
        return optionalCourseOneCompleted;
    }

    /**
     * @return the optionalCourseTwoCompleted
     */
    public boolean isOptionalCourseTwoCompleted() {
        return optionalCourseTwoCompleted;
    }

    /**
     * @return the optionalCourseThreeCompleted
     */
    public boolean isOptionalCourseThreeCompleted() {
        return optionalCourseThreeCompleted;
    }

    /**
     * @return the optionalCourseFourCompleted
     */
    public boolean isOptionalCourseFourCompleted() {
        return optionalCourseFourCompleted;
    }

    /**
     * @return the optionalCourseFiveCompleted
     */
    public boolean isOptionalCourseFiveCompleted() {
        return optionalCourseFiveCompleted;
    }

    /**
     * @return the practiceCompleted
     */
    public boolean isPracticeCompleted() {
        return practiceCompleted;
    }

    /**
     * @return the certificateCompleted
     */
    public boolean isCertificateCompleted() {
        return certificateCompleted;
    }

    /**
     * @return the evaluationCompleted
     */
    public boolean isEvaluationCompleted() {
        return evaluationCompleted;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the operator
     */
    public Long getOperator() {
        return operator;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param user the user to set
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * @param projectId the projectId to set
     */
    public void setProjectId(Long projectId) {
        this.projectId = projectId;
    }

    /**
     * @param selfAwarenessCompleted the selfAwarenessCompleted to set
     */
    public void setSelfAwarenessCompleted(boolean selfAwarenessCompleted) {
        this.selfAwarenessCompleted = selfAwarenessCompleted;
    }

    /**
     * @param requiredCourseOneCompleted the requiredCourseOneCompleted to set
     */
    public void setRequiredCourseOneCompleted(boolean requiredCourseOneCompleted) {
        this.requiredCourseOneCompleted = requiredCourseOneCompleted;
    }

    /**
     * @param requiredCourseTwoCompleted the requiredCourseTwoCompleted to set
     */
    public void setRequiredCourseTwoCompleted(boolean requiredCourseTwoCompleted) {
        this.requiredCourseTwoCompleted = requiredCourseTwoCompleted;
    }

    /**
     * @param requiredCourseThreeCompleted the requiredCourseThreeCompleted to set
     */
    public void setRequiredCourseThreeCompleted(boolean requiredCourseThreeCompleted) {
        this.requiredCourseThreeCompleted = requiredCourseThreeCompleted;
    }

    /**
     * @param requiredCourseFourCompleted the requiredCourseFourCompleted to set
     */
    public void setRequiredCourseFourCompleted(boolean requiredCourseFourCompleted) {
        this.requiredCourseFourCompleted = requiredCourseFourCompleted;
    }

    /**
     * @param optionalCourseOneCompleted the optionalCourseOneCompleted to set
     */
    public void setOptionalCourseOneCompleted(boolean optionalCourseOneCompleted) {
        this.optionalCourseOneCompleted = optionalCourseOneCompleted;
    }

    /**
     * @param optionalCourseTwoCompleted the optionalCourseTwoCompleted to set
     */
    public void setOptionalCourseTwoCompleted(boolean optionalCourseTwoCompleted) {
        this.optionalCourseTwoCompleted = optionalCourseTwoCompleted;
    }

    /**
     * @param optionalCourseThreeCompleted the optionalCourseThreeCompleted to set
     */
    public void setOptionalCourseThreeCompleted(boolean optionalCourseThreeCompleted) {
        this.optionalCourseThreeCompleted = optionalCourseThreeCompleted;
    }

    /**
     * @param optionalCourseFourCompleted the optionalCourseFourCompleted to set
     */
    public void setOptionalCourseFourCompleted(boolean optionalCourseFourCompleted) {
        this.optionalCourseFourCompleted = optionalCourseFourCompleted;
    }

    /**
     * @param optionalCourseFiveCompleted the optionalCourseFiveCompleted to set
     */
    public void setOptionalCourseFiveCompleted(boolean optionalCourseFiveCompleted) {
        this.optionalCourseFiveCompleted = optionalCourseFiveCompleted;
    }

    /**
     * @param practiceCompleted the practiceCompleted to set
     */
    public void setPracticeCompleted(boolean practiceCompleted) {
        this.practiceCompleted = practiceCompleted;
    }

    /**
     * @param certificateCompleted the certificateCompleted to set
     */
    public void setCertificateCompleted(boolean certificateCompleted) {
        this.certificateCompleted = certificateCompleted;
    }

    /**
     * @param evaluationCompleted the evaluationCompleted to set
     */
    public void setEvaluationCompleted(boolean evaluationCompleted) {
        this.evaluationCompleted = evaluationCompleted;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param operator the operator to set
     */
    public void setOperator(Long operator) {
        this.operator = operator;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
}
